package fr.enseirbmatmeca.apms.spring.model;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.ElementCollection;

import org.hibernate.annotations.GenericGenerator;

@SuppressWarnings("serial")
@Entity
public class User implements Serializable {

	private String id;
	private String login;
	private String password;
	private String authToken;

	public User() {
		
	} 
	
	public User(String login, String password){
	this.login = login;
	this.password = password;
	generateAuthToken();
	}

	@Id
	@GeneratedValue(generator="system-uuid")
	@GenericGenerator(name="system-uuid", strategy = "uuid")
	@Column(name = "id_user", unique = true, nullable = false)
	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Column(name = "login_user", nullable = false)
	public String getLogin() {
		return login;
	}
	
	public void setLogin(String login) {
		this.login = login;
	}
	
	@Column(name = "authtoken_user")
	public String getAuthToken() {
		return authToken;
	}

	public void setAuthToken(String authToken) {
		this.authToken = authToken;
	}
	
	@Column(name = "password_user")
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	private void generateAuthToken(){
		try {
		      //Initialize SecureRandom
		      //This is a lengthy operation, to be done only upon
		      //initialization of the application
		      SecureRandom prng = SecureRandom.getInstance("SHA1PRNG");

		      //generate a random number
		      String randomNum = new Integer( prng.nextInt() ).toString();

		      //get its digest
		      MessageDigest sha = MessageDigest.getInstance("SHA-1");
		      byte[] result =  sha.digest( randomNum.getBytes() );

		      setAuthToken(hexEncode(result));
		    }
		    catch ( NoSuchAlgorithmException ex ) {
		      System.err.println(ex);
		    }
	}
	
	
	/**
	  * The byte[] returned by MessageDigest does not have a nice
	  * textual representation, so some form of encoding is usually performed.
	  *
	  * This implementation follows the example of David Flanagan's book
	  * "Java In A Nutshell", and converts a byte array into a String
	  * of hex characters.
	  *
	  * Another popular alternative is to use a "Base64" encoding.
	  */
	  static private String hexEncode( byte[] aInput){
	    StringBuilder result = new StringBuilder();
	    char[] digits = {'0', '1', '2', '3', '4','5','6','7','8','9','a','b','c','d','e','f'};
	    for ( int idx = 0; idx < aInput.length; ++idx) {
	      byte b = aInput[idx];
	      result.append( digits[ (b&0xf0) >> 4 ] );
	      result.append( digits[ b&0x0f] );
	    }
	    return result.toString();
	  }
	
// Pour garder un exemple
//	@OneToMany(targetEntity=P2PTransaction.class, mappedBy="user", fetch=FetchType.LAZY, cascade = CascadeType.MERGE)
//	public List<P2PTransaction> getP2pTransactions() {
//		return P2pTransactions;
//	}
//
//	
//	public void setP2pTransactions(List<P2PTransaction> p2pTransactions) {
//		P2pTransactions = p2pTransactions;
//	}
	
}
